package com.aldobucchi.x.flash.display
{



	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	


	public class DisplayObjectUtil
	{
		


		public static function getFirstCommonAncestor( o1:DisplayObject, o2:DisplayObject ):DisplayObjectContainer
		{
			// TODO: optimize
			var a1:Array = getAncestors( o1 );
			var a2:Array = getAncestors( o2 );
			for each ( var a:* in a1 )
				if ( a2.indexOf( a ) != -1 )
					return a;
			return null;
		}

		
		
		/**
		 * 
		 * Returns all ancestors in parent, grandparent, etc order.
		 * 
		 * @param o
		 * @return 
		 * 
		 */		
		public static function getAncestors( o:DisplayObject ):Array
		{
			var arr:Array = [ ];
			while ( o.parent != null )
			{
				o = o.parent;
				arr.push( o );		
			}
			return arr;
		}

	}
}